package com.gagakuai.bpm.service.scheduleTask.impl;

import com.gagakuai.bpm.config.schleTask.ScheduleTaskConfig;
import com.gagakuai.bpm.service.scheduleTask.ScheduleTaskService;
import com.gagakuai.bpm.service.scheduleTask.Task;

import com.gagakuai.bpm.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Slf4j
@Service("scheduleTaskServiceImpl")
public class ScheduleTaskServiceImpl implements ScheduleTaskService {

    private final ScheduleTaskConfig scheduleTaskConfig;

    private final ThreadPoolTaskScheduler threadPoolTaskScheduler;

    private final Task task;

    public ScheduleTaskServiceImpl (ScheduleTaskConfig scheduleTaskConfig, ThreadPoolTaskScheduler threadPoolTaskScheduler, Task task){
        this.scheduleTaskConfig = scheduleTaskConfig;
        this.threadPoolTaskScheduler = threadPoolTaskScheduler;
        this.task = task;
        ScheduleTaskService.scheduleTaskServiceMap.put("scheduleTaskServiceImpl", this);
    }

    @PostConstruct
    @Override
    public boolean startCorn() {
        if(StringUtils.isBlank(scheduleTaskConfig.getCorn())){
            return false;
        }

        if(StringUtils.isBlank(scheduleTaskConfig.getCorn())){
            log.error("错误：未配制定时任务的CORN表达式");
            return false;
        }

        threadPoolTaskScheduler.schedule(task, new CronTrigger(scheduleTaskConfig.getCorn()));
        log.info("**********");
        log.info("同步用户信息定时任务已启动");
        return true;
    }

    @Override
    public boolean stopCorn() {
        return false;
    }


    public static void main(String[] args) {




    }
}
